#include <stdio.h>
#include <string.h>

int main()
{
    int count = 0;
    while(1)
    {
        FILE* fd = fopen("tmp.txt", "w+");
        if(!fd)
        {
            printf("%d\n",count);
            perror("fopen");
            return 0;
        }
        count++;
    }
    FILE* fd = fopen("tmp.txt", "w+");
    if(!fd)
    {
        perror("fopen");
        return -1;
    }
    printf("fopen success\n");

    const char* arr = "chenlin666";
    //int ret = fwrite(arr, 2, 5, fd);
    int ret = fwrite(arr, 1, strlen(arr), fd);
    printf("ret = [%d]\n", ret);

    fseek(fd, 0, SEEK_SET);

    char buffer[1024] = {0};
    //int size = fread(buffer, 1023, 1, fd);
    int size = fread(buffer, 1, sizeof(buffer) - 1, fd);
    if(size <= 0)
    {
        perror("fread");
        return -1;
    }
    printf("%s\n", buffer);

    return 0;
}
